package org.study.javafx.study01.tree.code;

import cn.hutool.core.io.resource.ClassPathResource;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;
import org.study.javafx.study01.AppStarter;
import org.study.javafx.study01.config.AppConfig;

import java.util.Arrays;
import java.util.List;

/**
 * 参考 https://blog.csdn.net/zy103118/article/details/122573538
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2024年08月18日
 */
@Slf4j
public class TreeCode1_2 extends Application {
    /**
     * 直接运行此方法会报如下错误：
     * 错误: 缺少 JavaFX 运行时组件, 需要使用该组件来运行此应用程序
     * 请运行 {@link AppStarter#main(String[])}
     */
    public static void main(String[] args) {
        AppConfig.init();
        launch(args);
    }

    /**
     * 创建一个图片节点类型对象
     */
    private final Node rootIcon = new ImageView(new Image(new ClassPathResource("icon/icon.png").getPath()));
    private final Node depIcon = new ImageView(new Image(new ClassPathResource("icon/icon.png").getPath()));
    /**
     * 创建员工列表对象
     */
    List<Employee> employees = Arrays.asList(
            new Employee("Jacob Smith", "Accounts Department"),
            new Employee("Isabella Johnson", "Accounts Department"),
            new Employee("Ethan Williams", "Sales Department"),
            new Employee("Emma Jones", "Sales Department"),
            new Employee("Michael Brown", "Sales Department"),
            new Employee("Anna Black", "Sales Department"),
            new Employee("Rodger York", "Sales Department"),
            new Employee("Susan Collins", "Sales Department"),
            new Employee("Mike Graham", "IT Support"),
            new Employee("Judy Mayer", "IT Support"),
            new Employee("Gregory Smith", "IT Support")
    );
    /**
     * 根节点选项元素
     */
    TreeItem<String> rootNode;

    @Override
    public void init() throws Exception {
        super.init();
        // 初始化树形结构根节点的方法
        this.rootNode = new TreeItem<>("根节点", rootIcon);
    }

    @Override
    public void start(Stage stage) throws Exception {
        // 设置根节点扩展属性
        rootNode.setExpanded(true);
        for (Employee employee : employees) {
            // 树根节点选项
            TreeItem<String> empLeaf = new TreeItem<>(employee.getName());
            boolean found = false;
            for (TreeItem<String> depNode : rootNode.getChildren()) {
                if (depNode.getValue().contentEquals(employee.getDepartment())) {
                    // 对应部门下添加对应的员工节点
                    depNode.getChildren().add(empLeaf);
                    found = true;
                    break;
                }
            }
            if (!found) {
                // 创建一个部门级别的节点对象
                TreeItem<String> depNode = new TreeItem<>(employee.getDepartment(), depIcon);
                // 根节点上添加部门节点
                rootNode.getChildren().add(depNode);
                // 部门节点上添加员工节点
                depNode.getChildren().add(empLeaf);
            }
        }
        stage.setTitle("树形结构标例");
        // 创建垂直盒子模型
        VBox box = new VBox();
        // 创建场景对象
        final Scene scene = new Scene(box, 400, 300);
        // 场景填充背景色
        scene.setFill(Color.LIGHTGRAY);
        // 给树形结构添加根节点
        TreeView<String> treeView = new TreeView<>(rootNode);
        // 垂直布局对象上面添加一个树形结构节点
        box.getChildren().add(treeView);
        // 舞台设置场景
        stage.setScene(scene);
        // 舞台播放
        stage.show();
    }

    @Override
    public void stop() throws Exception {
        super.stop();
    }

    static class Employee {
        /**
         * mvvm机制下的和String很像的数据类型SimpleStringProperty
         */
        private final SimpleStringProperty name;
        private final SimpleStringProperty department;

        /**
         * mvvm机制下的和String很像的数据类型SimpleStringProperty
         */
        private Employee(String name, String department) {
            this.name = new SimpleStringProperty(name);
            this.department = new SimpleStringProperty(department);
        }

        public String getName() {
            return name.get();
        }

        public void setName(String fName) {
            name.set(fName);
        }

        public String getDepartment() {
            return department.get();
        }

        public void setDepartment(String fName) {
            department.set(fName);
        }
    }
}
